package service

import (
	"context"
	"errors"
	"insighttracking/query/dao"
	"insighttracking/query/model"
)

type _shelfGridService struct{}

func (s _shelfGridService) CreateApi(ctx context.Context, req model.ShelfGridEntity) error {
	err := dao.ShelfGridEntity.WithContext(ctx).Create(&req)
	if err != nil {
		return errors.New("创建失败")
	}
	return nil
}

func (s _shelfGridService) BatchCreateApi(ctx context.Context, req []*model.ShelfGridEntity) error {
	err := dao.ShelfGridEntity.WithContext(ctx).CreateInBatches(req, 100)
	if err != nil {
		return errors.New("创建失败")
	}
	return nil
}

func (s _shelfGridService) DeleteByIdApi(ctx context.Context, enterpriseId int64, id int64) error {
	if _, err := dao.ShelfGridEntity.WithContext(ctx).Where(dao.ShelfGridEntity.EnterpriseID.Eq(enterpriseId), dao.ShelfGridEntity.ID.Eq(id)).Delete(); err != nil {
		return err
	}
	return nil
}

func (s _shelfGridService) GetListApi(ctx context.Context, enterpriseId int64, shelfId int64) ([]*model.ShelfGridEntity, error) {
	return dao.ShelfGridEntity.WithContext(ctx).Where(dao.ShelfGridEntity.EnterpriseID.Eq(enterpriseId), dao.ShelfGridEntity.ShlefID.Eq(shelfId), dao.ShelfGridEntity.Status.Eq(0)).Order(dao.ShelfGridEntity.ID.Asc()).Find()
}

func (s _shelfGridService) GetSameCodeApi(ctx context.Context, enterpriseId int64, shelfId int64, code []string) (*model.ShelfGridEntity, error) {
	return dao.ShelfGridEntity.WithContext(ctx).Where(
		dao.ShelfGridEntity.EnterpriseID.Eq(enterpriseId),
		dao.ShelfGridEntity.ShlefID.Eq(shelfId),
		dao.ShelfGridEntity.Code.In(code...),
	).First()
}

func (s _shelfGridService) GetByIdListApi(ctx context.Context, idList []int64) ([]*model.ShelfGridEntity, error) {
	return dao.ShelfGridEntity.WithContext(ctx).Where(dao.ShelfGridEntity.ID.In(idList...)).Find()
}

func (s _shelfGridService) GetCountByShelfIdApi(ctx context.Context, enterpriseId int64, shelfId int64) (int64, error) {
	return dao.ShelfGridEntity.WithContext(ctx).Where(dao.ShelfGridEntity.EnterpriseID.Eq(enterpriseId), dao.ShelfGridEntity.ShlefID.Eq(shelfId)).Count()
}

func (s _shelfGridService) GetAvailableGridApi(ctx context.Context, enterpriseId int64, shelfId int64) (int64, error) {
	return dao.ShelfGridEntity.WithContext(ctx).Where(dao.ShelfGridEntity.EnterpriseID.Eq(enterpriseId), dao.ShelfGridEntity.ShlefID.Eq(shelfId)).Count()
}

func (s _shelfGridService) GetListByWarehouseIDApi(ctx context.Context, enterpriseId int64, warehouseID int64) ([]*model.ShelfGridEntity, error) {
	return dao.ShelfGridEntity.WithContext(ctx).Where(
		dao.ShelfGridEntity.EnterpriseID.Eq(enterpriseId),
		dao.ShelfGridEntity.WarehouseID.Eq(warehouseID),
		dao.ShelfGridEntity.Status.Eq(0),
	).Order(dao.ShelfGridEntity.ID.Asc()).Find()
}

func (s _shelfGridService) GetGridsByWarehouseIDApi(ctx context.Context, enterpriseId int64, warehouseID int64) (grids []int64, err error) {
	err = dao.ShelfGridEntity.WithContext(ctx).Where(
		dao.ShelfGridEntity.EnterpriseID.Eq(enterpriseId),
		dao.ShelfGridEntity.WarehouseID.Eq(warehouseID),
		dao.ShelfGridEntity.Status.Eq(0),
	).Pluck(dao.ShelfGridEntity.ID, &grids)
	return
}

var ShelfGridService = new(_shelfGridService)
